package cn.edu.fzu.homemaking.company.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;

import cn.edu.fzu.homemaking.company.api.SsoApi;
import cn.edu.fzu.homemaking.company.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.fzu.homemaking.company.domain.enums.UserTypeEnum;
import cn.edu.fzu.homemaking.company.domain.enums.SexEnum;
import cn.edu.fzu.homemaking.company.domain.enums.StatusEnum;
import cn.edu.fzu.homemaking.web.Result;

/**
 * @author wanjie
 * @version 1.0
 */
@RestController
@RequestMapping("/api/company/auth/user")
@Api(tags = "公司端人员相关接口")
public class CompanyUserController {

    @Resource
    private SsoApi ssoApi;

    @PostMapping("/insert")
    @ApiOperation(value = "新增员工", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", required = true),
            @ApiImplicitParam(name = "email", value = "电子邮箱", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
            @ApiImplicitParam(name = "sex", value = "性别 0=女，1=男", required = true),
            @ApiImplicitParam(name = "birth", value = "出生日期生日：yyyy-MM-dd", required = true),
            @ApiImplicitParam(name = "phone", value = "电话号码", required = true),
            @ApiImplicitParam(name = "address", value = "家庭地址", required = true),
            @ApiImplicitParam(name = "faceUrl", value = "用户人脸url"),
            @ApiImplicitParam(name = "labelId", value = "服务标签", required = true),
            @ApiImplicitParam(name = "salary", value = "薪水", required = true),
            @ApiImplicitParam(name = "type", required = false, value = "公司员工类型"),})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result insert(String name, String email, String password, SexEnum sex, String phone, String birth,
                         String address, String faceUrl, Long labelId, BigDecimal salary, UserTypeEnum type) {
        if (type == UserTypeEnum.ADMIN) {
            throw new ServiceException("不能添加管理员");
        }
        return ssoApi.addCompanyUser(email, password, name, sex, birth, phone, address, faceUrl, labelId, salary, type);
    }


    @GetMapping("/list")
    @ApiOperation(value = "所有的员工展示列表", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "email", required = false, value = "电子邮件"),
            @ApiImplicitParam(name = "name", required = false, value = "姓名"),
            @ApiImplicitParam(name = "sex", required = false, value = "性别 0=女，1=男"),
            @ApiImplicitParam(name = "phone", required = false, value = "电话号码"),
            @ApiImplicitParam(name = "status", required = false, value = "状态，0=正常，1=禁用，-1=删除"),
            @ApiImplicitParam(name = "companyId", required = false, value = "公司标签"),
            @ApiImplicitParam(name = "type", required = false, value = "公司员工类型"),
            @ApiImplicitParam(name = "labelId", required = false, value = "标签Id"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(String email, String name, SexEnum sex, String phone, StatusEnum status, Long companyId,
                       UserTypeEnum type, Long labelId, Integer pageNum, Integer pageSize) {
        return ssoApi.listCompanyUser(email, name, sex, phone, status, companyId, type, labelId, pageNum, pageSize);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改员工信息", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "状态，0=正常，1=禁用，-1=删除"),
            @ApiImplicitParam(name = "email", value = "电子邮件"), @ApiImplicitParam(name = "labelId", value = "服务标签Id"),
            @ApiImplicitParam(name = "faceUrl", value = "人脸照片路径"), @ApiImplicitParam(name = "phone", value = "电话号码"),
            @ApiImplicitParam(name = "address", value = "地址")})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateDemandStatus(Long companyUserId, StatusEnum status, String email, Long labelId, String faceUrl,
                                     String phone, String address) {
        return ssoApi.updateCompanyUser(companyUserId, email, phone, address, faceUrl, status, labelId);

    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除员工", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "email", value = "email", required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result delete(Long companyUserId) {
        return ssoApi.updateCompanyUser(companyUserId, null, null, null, null, StatusEnum.DELETE, null);
    }

}
